<template>
  <div class="shop_container">
    <div class="shop_list">
      <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getGoodsSelected[item.id]"
              @change="selectedChanged(item.id, $store.getters.getGoodsSelected[item.id])"
            ></mt-switch>
            <img :src="item.thumb_path">
            <div class="info">
              <p class="title">{{ item.title }}</p>
              <p class="price">
                <span>￥{{ item.sell_price }}</span>
                <!-- nunbox -->
                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsId="item.id"></numbox>
                <a href="#" @click.prevent="remove(item.id, i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计（不包含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{ $store.getters.getGoodsCountAndAmount.count }}</span> 件，总价
              <span class="red">￥{{ $store.getters.getGoodsCountAndAmount.amount }}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    <div>{{ $store.getters.getGoodsSelected }}</div>
  </div>
</template>

<script>
import numbox from "../subcomponents/shopcar_numbox.vue";
export default {
  components: {
    numbox
  },
  name: "app",
  data() {
    return {
      goodslist: []
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      // 获取到 store 中所有 的商品的id 然后拼接出一个 用逗号分隔的字符串
      var idArr = [];
      this.$store.state.car.forEach(item => {
        idArr.push(item.id);
      });
      if (idArr.length <= 0) {
        // 如果 购物车中没有商品，则直接返回，不需要返回 数据接口，否则会报错
        return;
      }
      this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then(res => {
          console.log(res);
          if (res.body.status === 0) {
            this.goodslist = res.body.message;
          }
        });
    },
    remove(id, index) {
      this.goodslist.splice(index, 1);
      this.$store.commit("remove", id);
    },
    selectedChanged(id, value) {
      console.log(id + "---------" + value);
      this.$store.commit("updateGoodsSelected", { id: id, selected: value });
    }
  }
};
</script>

<style lang="scss" scoped>
.shop_container {
  padding: 3px 0;
  .shop_list {
    .mui-card-content-inner {
      // .mint-switch {
      //   // height: 25px;
      //   background: red;
      //   .mint-switch-input {
      //    > span{
      //      &.mint-switch-core {
      //       width: 20px;
      //     }
      //    }
      //   }
      // }
      .mui-numbox {
        height: 25px;
        // width: 87px;
        // padding: 0 25px 0 25px;
        [type="button"] {
          &.mui-btn-numbox-plus {
            // width:25px;
          }
        }
      }
      display: flex;
      align-items: center;
      justify-content: space-between;
      img {
        width: 50px;
        height: 50px;
      }
      .info {
        .title {
          font-size: 14px;
          color: #666;
          font-weight: bold;
        }
        .price {
          span {
            color: red;
            font-size: 14px;
            font-weight: 600;
          }
        }
      }
    }
    overflow: hidden;
  }
  background-color: #eee;
}
.jiesuan {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .red {
    font-size: 14px;
    color: red;
    font-weight: 700;
  }
}
</style>